package com.example.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.bean.Department;
import com.example.server.util.RespBean;
import com.example.server.dao.DepartmentDao;
import com.example.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 别怿言
 * @since 2023-02-03
 */
@Service
@Transactional
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, Department> implements IDepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Department> getAllDepartments() {
        return departmentDao.getAllDepartments(-1);
    }

    @Override
    public RespBean addDepartment(Department department) {
        department.setEnabled(true);
        departmentDao.addDepartment(department);
        if(1==department.getResult()){
            return RespBean.success("添加成功！",department);
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public RespBean deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentDao.deleteDepartment(department);
        if(-2==department.getResult()){
            return RespBean.error("该部门还有子部门，删除失败！");
        }
        if(-1==department.getResult()){
            return RespBean.error("该部门还有员工，删除失败！");
        }
        if(1==department.getResult()){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
}
